<?php 


class Projekt {

function lang()
{
include_once 'admin/config.php';
$conn = db();

$result = pg_query($conn,"SELECT * FROM catroid_lang");

while ($row = pg_fetch_object($result)) {
    echo '<option value="'.$row->id.'">'.$row->lang.'</option>'; 
}

return true;

}

function pros() 
{
include_once 'admin/config.php';
$conn = db();

$result = pg_query($conn,"SELECT * FROM projects");

while ($row = pg_fetch_object($result)) {
    echo '<option value="'.$row->id.'">'.$row->name.'</option>'; 
}

return true;

}

function getNames($proj,$lang,$user)
{
include_once 'admin/config.php';
$conn = db();
$result = pg_query($conn,"SELECT * FROM catroid_words WHERE lang_id = '$lang' AND project_id = '$proj' ORDER BY id ASC");

while ($row = pg_fetch_object($result)) {
    echo '<tr>';
	echo '<td><form action="index.php" method="POST">';
    echo $row->name; 
	echo '</td><td>';
	$result2 = pg_query($conn,"SELECT * FROM suggestions WHERE word_id = '$row->id'");
	while($row2 = pg_fetch_object($result2))
	{
	echo $row2->name;
	if ($user == true)
	{ 
	echo " ";
	echo '<a href="#" onclick="like_add('.$row2->id.','.$row->id.',1,'.$_SESSION['auth_id'].')"><img src="icons/plus.png"></a>';
	echo " ";
	echo '<a href="#" onclick="like_add('.$row2->id.','.$row->id.',2,'.$_SESSION['auth_id'].')"><img src="icons/minus.png"></a>';
	}
    echo " ";
	echo '<span id="feedback_'.$row2->id.'_'.$row->id.'">'.$row2->feedback.'</span>';
	echo "; ";
	} 
	echo '&nbsp;</td>'; 
	echo '<td><input type="text" name="new"><input type="hidden" name="projekt" value="projekt">';
	echo '<input type="hidden" name="word_id" value="'.$row->id.'">';
	echo '<input type="hidden" name="proj" value="'.$proj.'">';
	echo '<input type="hidden" name="lang" value="'.$lang.'">';
	echo '</td> <td><input type="submit" name="hinzu" value="Hinzuf&uuml;gen">';
	echo '</form></td></tr>';
}


}

function getTable($proj,$lang)
{
include_once 'admin/config.php';
$conn = db();
$result = pg_query($conn,"SELECT * FROM catroid_words WHERE lang_id = '$lang' AND project_id = '$proj' ORDER BY id ASC");

$rows = pg_num_rows($result);

if ($rows >= 1)
{
 return true;
} else {
  return false;

}

}

function getVote($vote)
{
include_once 'admin/config.php';
$conn = db();
$result = pg_query($conn,"SELECT * FROM suggestions WHERE id = '$vote'");
$rows = pg_num_rows($result);
if ($rows >= 1)
{
return true;
} else {
return false;
}
}

function addVote($vote,$math,$user_id)
{
include_once 'admin/config.php';
$conn = db();
if ($math == 1)
{
$result = pg_query($conn,"UPDATE suggestions Set feedback = feedback+1  WHERE id = '$vote'");
} else {
$result = pg_query($conn,"UPDATE suggestions Set feedback = feedback-1 WHERE id = '$vote'");
}

$check = pg_affected_rows($result);
if ($check == true)
{
 $result = pg_query($conn,"INSERT INTO votes (id_vote,id_user) VALUES ('$vote','$user_id')");
 return true;
} else {
return false;
}

}

function resVote($vote)
{
include_once 'admin/config.php';
$conn = db();
$result = pg_query($conn,"SELECT * FROM suggestions WHERE id = '$vote'");

$rows = pg_num_rows($result);
if ($rows >= 1)
{
$feedback = pg_fetch_object($result);
return $feedback->feedback;
} else {
return "FALSE";
}

}//end getVote


function newWord($word_id,$new)
{
if ($this->checkWord($new) == false)
{
return false;
}
include_once 'admin/config.php';
$conn = db();
$result = pg_query($conn, "INSERT INTO suggestions (word_id,name,feedback) VALUES ('$word_id','$new',0)"); 
$check = pg_affected_rows($result);
if ($check == true)
{
 return true;
} else {
return false;
}
}

function checkWord($new)
{
include_once 'admin/config.php';
$conn = db();
$result = pg_query($conn, "SELECT * FROM suggestions WHERE name = '$new'"); 
$rows = pg_num_rows($result);
if ($rows >= 1)
{
?>
<script>
alert("Wort schon vorhanden");
</script>
<?php 
 return false;
} else {
 return true;
}
}

function getCount($user_id, $vote)
{
include_once 'admin/config.php';
$conn = db();
$result = pg_query($conn, "SELECT * FROM votes WHERE id_vote = '$vote' AND id_user = '$user_id'"); 
$rows = pg_num_rows($result);
if ($rows >= 1)
{
 return false;
} else {
 return true;
}

}

function getSug($lang,$proj)
{
include_once 'admin/config.php';
$conn = db();
$result = pg_query($conn,"SELECT * FROM catroid_words WHERE lang_id = '$lang' AND project_id = '$proj' ORDER BY id ASC");

while ($row = pg_fetch_object($result)) {
 echo '<tr>';
	echo '<td><form action="index.php" method="POST">';
	echo '<span id="word_'.$row->id.'">';
    echo $row->name; 
	echo '</span>';
	echo '</td><td>';
	$result2 = pg_query($conn,"SELECT * FROM suggestions WHERE word_id = '$row->id'");
	while($row2 = pg_fetch_object($result2))
	{	
    echo " ";
	echo '<span id="sug_'.$row2->id.'_'.$row->id.'">
	<a href="#" onclick="delete_com('.$row2->id.','.$row->id.')"> D </a> 
	<a href="#" onclick="hinzu('.$row2->id.','.$row->id.',&quot;'.$row2->name.'&quot;)"> H </a>
	'.$row2->name.' '.$row2->feedback.';</span>';
	 
	} 
	echo '&nbsp;</td>'; 

}

} 

function deleteSug($id)
{
include_once 'admin/config.php';
$conn = db();
$result = pg_query($conn, "DELETE FROM suggestions WHERE id = '$id'"); 
$check = pg_affected_rows($result);
if ($check == true)
{
 return true;
} else {
return false;
}

}

function addSug($id,$row,$name)
{
include_once 'admin/config.php';
$conn = db();
$result = pg_query($conn, "UPDATE catroid_words Set name = '$name' WHERE id = '$row'"); 
$check = pg_affected_rows($result);
if ($check == true)
{
  $result = pg_query($conn, "DELETE FROM suggestions WHERE id = '$id'"); 
 return true;
} else {
return false;
} 
 
} 

function lang_w()
{
include_once 'admin/config.php';
$conn = db();

$result = pg_query($conn,"SELECT * FROM catroid_lang");

while ($row = pg_fetch_object($result)) {
    echo '<option value="'.$row->id.'">'.$row->lang.' - W&ouml;rter ausgeben</option>'; 
}
}

function output_lang($lang)
{
include_once 'admin/config.php';
$conn = db();
if ($lang != 0)
{
$result = pg_query($conn,"SELECT * FROM catroid_words WHERE lang_id = '$lang'");
} else {
$result = pg_query($conn,"SELECT * FROM catroid_words");
}
$rows = pg_num_rows($result);
if ($rows >= 1)
{
echo '<ul>';
while ($row = pg_fetch_object($result)) {
    echo '<li>'.$row->name.'</li>'; 
}
echo '</ul>';
} else {
echo 'No Words!';
}

}

function insertTyp($typ)
{
include_once 'admin/config.php';
$conn = db();
if ($this->checkTyp($typ) == true)
{
$result = pg_query($conn, "INSERT INTO catroid_typ (string_name,sortierung) VALUES ('$typ',99)"); 
$check = pg_affected_rows($result);
if ($check == true)
{
 return true;
} else {
return false;
}
} else {
return false;
}

}

function checkTyp($typ)
{
include_once 'admin/config.php';
$conn = db();
$result = pg_query_params($conn, 'SELECT id FROM catroid_typ WHERE string_name = $1', array($typ) );
$rows = pg_num_rows($result);
if ($rows == 0)
{
 return true;
} else {
 return false;
}

}

function getAllW($option)
{
include_once 'admin/config.php';
$conn = db();
$result = pg_query($conn,"SELECT * FROM catroid_typ");

if ($option == 0)
	 {
while ($row = pg_fetch_object($result)) {
     
    echo '<option value="'.$row->id.'">'.$row->string_name.'</option>'; 	
}
} else {

while ($row = pg_fetch_object($result)) {
	echo '<li>'.$row->string_name.'</li>';
	}
}


}//ende function getAllW


function deleteTyp($id)
{
include_once 'admin/config.php';
$conn = db();
$result = pg_query($conn, "DELETE FROM catroid_typ WHERE id = '$id'"); 
$check = pg_affected_rows($result);
if ($check == true)
{
 return true;
} else {
return false;
}
}//ende deleteTyp




function insertWord($word,$lang,$proj,$app)
{
include_once 'admin/config.php';
$conn = db();
$check  = $this->checkWords($word,$lang,$proj,$app);
if ($check == true)
{
$result = pg_query($conn, "INSERT INTO catroid_words (st_na_id,lang_id,user_id,name,likes,aktiv,project_id) VALUES ('$app','$lang',1, '$word',1,1,'$proj')"); 
$check = pg_affected_rows($result);
if ($check == true)
{
 return true;
} else {
return false;
}
} else {
return false;
}
}//ende insertWord

function checkWords($word,$lang,$proj,$app)
{
include_once 'admin/config.php';
$conn = db();
$result = pg_query_params($conn, 'SELECT id FROM catroid_words WHERE name = $1 AND lang_id = $2 AND project_id = $3 AND st_na_id = $4', array($word,$lang,$proj,$app) );
$rows = pg_num_rows($result);
if ($rows == 0)
{
 $result2 = pg_query_params($conn, 'SELECT id FROM catroid_words WHERE lang_id = $1 AND project_id = $2 AND st_na_id = $3', array($lang,$proj,$app) );
 $row2 = pg_fetch_object($result2);
 $result3 = pg_query($conn, "DELETE FROM catroid_words WHERE id = '$row2->id'"); 
 return true;
} else {
 return false;
}

}//ends checkWords


function newLang($word)
{
include_once 'admin/config.php';
$conn = db();
$check  = $this->checkLang($word);
if ($check == true)
{
$result = pg_query($conn, "INSERT INTO catroid_lang (lang) VALUES ('$word')"); 
$check = pg_affected_rows($result);
if ($check == true)
{
 return true;
} else {
return false;
}
} else {
return false;
}

}

function checkLang($word)
{
include_once 'admin/config.php';
$conn = db();
$result = pg_query_params($conn, 'SELECT id FROM catroid_lang WHERE lang = $1', array($word) );
$rows = pg_num_rows($result);
if ($rows == 0)
{
 return true;
} else {
 return false;
}
}


function newProj($proj)
{
include_once 'admin/config.php';
$conn = db();
$check  = $this->checkProj($proj);
if ($check == true)
{
$result = pg_query($conn, "INSERT INTO projects (name) VALUES ('$proj')"); 
$check = pg_affected_rows($result);
if ($check == true)
{
 return true;
} else {
return false;
}
} else {
return false;
}

}

function checkProj($proj)
{
include_once 'admin/config.php';
$conn = db();
$result = pg_query_params($conn, 'SELECT id FROM projects WHERE name = $1', array($proj) );
$rows = pg_num_rows($result);
if ($rows == 0)
{
 return true;
} else {
 return false;
}
}

function deleteLang($lang)
{
include_once 'admin/config.php';
$conn = db();
$result = pg_query($conn, "DELETE FROM catroid_lang WHERE id = '$lang'"); 
$check = pg_affected_rows($result);
if ($check == true)
{
 return true;
} else {
return false;
}

}

function deleteProj($proj)
{
include_once 'admin/config.php';
$conn = db();
$result = pg_query($conn, "DELETE FROM projects WHERE id = '$proj'"); 
$check = pg_affected_rows($result);
if ($check == true)
{
 return true;
} else {
return false;
}

}


   
}

?>